package com.hongyi.modelcontest.core.util;

import com.sun.mail.util.MailSSLSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

@Service
public class SendEmail {
    private static final Logger logger = LoggerFactory.getLogger(SendEmail.class);

    @Value("${spring.mail.account}")
    private String account;//登录用户名
    @Value("${spring.mail.pass}")
    private String pass;        //登录密码
    @Value("${spring.mail.from}")
    private String from;        //发件地址
    @Value("${spring.mail.host}")
    private String host;        //服务器地址
    @Value("${spring.mail.port}")
    private String port;        //端口
    @Value("${spring.mail.protocol}")
    private String protocol; //协议

    //用户名密码验证，需要实现抽象类Authenticator的抽象方法PasswordAuthentication
    static class MyAuthenricator extends Authenticator {
        String u;
        String p;

        MyAuthenricator(String u, String p) {
            this.u = u;
            this.p = p;
        }

        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(u, p);
        }
    }

    @Async
    public void sendPdfEmail(String email, String url, String recordId) throws javax.mail.MessagingException {
        String title = "第二医学意见";
        String content = "亲爱的客户，</br>" +
                "   您好！您的第二医学意见申请单【" + recordId + "】，您选择的专家向您出具第二医学意见。本次服务已完成。</br>" +
                "点击链接查看意见书PDF若无法跳转，请复制以下链接到浏览器进行访问：<a href=\"" + url + "\" >" + url + "</a></br>" +
                "感谢您的支持！";
        //建立邮件消息
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        Properties prop = new Properties();
        //协议
        prop.setProperty("mail.transport.protocol", protocol);
        //服务器
        prop.setProperty("mail.smtp.host", host);
        //端口
        prop.setProperty("mail.smtp.port", port);
        //使用smtp身份验证
        prop.setProperty("mail.smtp.auth", "true");
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
        Session session = Session.getDefaultInstance(prop, new MyAuthenricator(account, pass));
        session.setDebug(true);
        MimeMessage mimeMessage = new MimeMessage(session);
        mimeMessage.setFrom(new InternetAddress(from));
        mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
        mimeMessage.setSubject(title);
        mimeMessage.setSentDate(new Date());
        mimeMessage.setContent(content, "text/html;charset=GBK");
        mimeMessage.saveChanges();
        Transport.send(mimeMessage);
        logger.info("邮件发送成功:{}", email);
    }

}
